//
//  WHContactsEditCell.m
//  AddressBook
//
//  Created by  on 2020/7/28.
//  Copyright © 2020 wh. All rights reserved.
//

#import "WHContactsEditCell.h"
#import "MacroDefinition.h"
#import "Masonry.h"
#import "WHSingleton.h"
#import "WHPhoneRegularExpression.h"
#import "WHAlertTool.h"

@interface WHContactsEditCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UITextField *contentTextField;

@end

@implementation WHContactsEditCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initWithView];
    }
    return self;
}

- (void)initWithView
{
    [self.contentView addSubview:self.titleLabel];
    [self addSubview:self.contentTextField];
    self.titleLabel.frame = CGRectMake(10, 5, 70, 50);
    self.contentTextField.frame = CGRectMake(85, 5, ScreenWidth-95, 50);
}

- (void)setDetailCellDataWithTitle:(NSString *)title content:(NSString *)content
{
    if ([title isEqualToString:content]) {
        self.titleLabel.text = title;
        self.contentTextField.placeholder = content;
    }else{
        self.titleLabel.text = title;
        self.contentTextField.text = content;
    }
}

- (void)textFieldTextDidChange:(UITextField *)textField{
    UITableViewCell *cell = (UITableViewCell *)[textField superview];
    UITableView *tableView = (UITableView *)[[textField superview] superview];
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
    [WHSingleton setDataWithIndex:indexPath.row content:self.contentTextField.text];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    UITableViewCell *cell = (UITableViewCell *)[textField superview];
    UITableView *tableView = (UITableView *)[[textField superview] superview];
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
    UIViewController *myViewController = [[UIApplication sharedApplication] delegate].window.rootViewController;
    BOOL isTrueNumber = [WHPhoneRegularExpression verificationPhoneWithNumber:textField.text];
    if (indexPath.row == 1 && !isTrueNumber) {
        __weak typeof(self)weakSelf = self;
        [WHAlertTool presentAlertWith:@"请输入正确的手机号码" controller:myViewController actionblock:^{
            weakSelf.contentTextField.text = @"";
            [WHSingleton setDataWithIndex:1 content:@""];
        }];
    }
    
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.textColor = UIColor.blackColor;
    }
    return _titleLabel;
}

- (UITextField *)contentTextField
{
    if (!_contentTextField) {
        _contentTextField = [[UITextField alloc] init];
        _contentTextField.delegate = self;
        [_contentTextField addTarget:self action:@selector(textFieldTextDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _contentTextField;
}
@end
